<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :search.sync="query"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" v-if="permission.iqcInspectionRecords_export" @click="handleExport">导出</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button size="small" type="text" v-if="permission.iqcInspectionRecords_report" @click="viewApaasExamineReport(row)">检验报告</el-button>
      </template>
    </avue-crud>
    <export-comp ref="exportComp" page-api-url="mes-qms/qms-iqc-record/page" :option="option" :page="page" :query="exportParams" file-name="来料检验记录"> </export-comp>
    <!--检验报告-->
    <el-dialog
      v-if="apaasInspectionReportDialog"
      fullscreen
      append-to-body
      :modal-append-to-body="true"
      :close-on-click-modal="false"
      title="检验报告"
      :visible.sync="apaasInspectionReportDialog"
    >
      <view-apaas-inspection-report :operateBtn="false" v-if="apaasInspectionReportDialog" ref="apaasReport"></view-apaas-inspection-report>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList } from '@/api/quality/iqc-inspection-records';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
import { filterEmptyValues } from '@/util/util';
import designerPreview from '@/views/designer/report.vue';
import viewApaasInspectionReport from '@/views/designer/preview';

export default {
  components: {
    exportComp,
    designerPreview,
    viewApaasInspectionReport
  },
  mixins: [],
  data() {
    return {
      pageCode: 'IQC_INSPECTED_RECORDS',
      apaasInspectionReportDialog: false,
      form: {},
      model: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      exportParams: {},
      selectionList: [],
      tableOption: {},
      data: [],
      option: {
        height: 'calc(100vh - 482px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        delBtn: false,
        editBtn: false,
        selection: true,
        viewBtn: false,
        viewBtnText: '详情',
        dialogClickModal: false,
        labelWidth: 120,
        searchLabelWidth: 90,
        // searchMenuSpan: 3,
        searchMenuPosition: 'right',
        menuWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '检验单号',
            prop: 'examineNo',
            search: true,
            searchOrder: 1,
            searchSpan: 5,
            rules: [
              {
                required: true,
                message: '请输入检验单号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '单据号',
            prop: 'purchaseOrderNo',
            search: true,
            searchOrder: 2,
            searchSpan: 5
          },
          {
            label: '物料编码',
            prop: 'partNo',
            searchOrder: 3,
            search: true,
            searchSpan: 5,
            rules: [
              {
                required: true,
                message: '请输入物料编码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '物料名称',
            prop: 'partName',
            search: true,
            searchOrder: 4,
            searchSpan: 5,
            rules: [
              {
                required: true,
                message: '请输入物料名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '物料规格',
            prop: 'partSpec',
            rules: [
              {
                required: true,
                message: '请输入物料规格',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '收料数量',
            prop: 'sendExamineQuantity'
          },
          {
            label: '检验数量',
            prop: 'examineQuantity',
            rules: [
              {
                required: true,
                message: '请输入检验数量',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '不合格率',
            prop: 'ngRate'
          },
          // {
          //   label: '退货数量',
          //   prop: 'disposeQuantity'
          // },
          // {
          //   label: '特采数量',
          //   prop: 'extractionQuantity'
          // },
          // {
          //   label: '实收数量',
          //   prop: 'realityQuantity'
          // },
          {
            label: '检验结果',
            prop: 'examineResult',
            type: 'select',
            searchSpan: 5,
            search: true,
            searchOrder: 6,
            dicData: [
              {
                label: '合格',
                value: 'OK'
              },
              {
                label: '不合格',
                value: 'NG'
              }
            ],
            rules: [
              {
                required: true,
                message: '请输入检验结果',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '不合格数量',
            prop: 'examineNgQuantity'
          },
          {
            label: '合格数量',
            prop: 'examineOkQuantity'
          },
          {
            label: '供应商编码',
            prop: 'vendorCode',
            searchOrder: 4,
            search: true,
            searchSpan: 5
          },
          {
            label: '供应商名称',
            prop: 'vendorName'
          },
          {
            label: '供应商批次',
            prop: 'vendorLotNo'
          },
          {
            label: '检验方式',
            prop: 'examineMode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=inspection_mode',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            rules: [
              {
                required: true,
                message: '请输入检验方式',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '检验人员',
            prop: 'examineByName'
          },
          {
            label: '检验日期',
            prop: 'examineTime',
            search: true,
            searchOrder: 20,
            searchSpan: 6,
            width: 150,
            type: 'datetime',
            searchType: 'datetimerange',
            order: 18,
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期'
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.iqcInspectionRecords_detail, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    viewApaasExamineReport(row) {
      if (!row.apaasExamineContent) {
        this.$message({
          type: 'warning',
          message: '检验报告信息不存在!'
        });
        return;
      }
      let examineContent = JSON.parse(row.apaasExamineContent || '{}');
      this.apaasInspectionReportDialog = true;
      this.$nextTick(() => {
        this.$refs.apaasReport.setDataJson(examineContent);
      });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.examineTime && params.examineTime.length > 0) {
        params.examineStartTime = params.examineTime[0];
        params.examineEndTime = params.examineTime[1];
      }
      delete params.examineTime;
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleExport() {
      this.exportParams = Object.assign(filterEmptyValues(this.query), {});
      this.$refs.exportComp.visible = true;
    },
    /**
     * 重新加载公用表格弹窗组件
     */
    newKey() {
      this.timer = new Date().getTime();
    }
  }
};
</script>

<style scoped lang="scss">
$size: 16px;
.view-style {
  font-weight: bold;
  font-size: $size;
}
.iqc-placeholder {
  font-size: $size;
  color: #aaaaaa;
}

.iqcDialog {
  ::v-deep input[type='file'] {
    display: none !important;
  }
}
$BORDER-STYLE: 1px solid #f0f0f0 !important;
::v-deep .inspection-btn:hover {
  background-color: #dedede;
}
::v-deep .el-dialog__header {
  border-bottom: $BORDER-STYLE;
}

::v-deep .el-dialog__footer {
  border-top: $BORDER-STYLE;
}

.fill-screen-icon {
  color: #909399;
  font-size: 15px;
  float: right;
  padding-right: 25px;
  &:hover {
    color: #409eff;
  }
}

.group-title {
  border-bottom: 1px solid rgb(240, 240, 240) !important;
  font-size: 16px;
  font-weight: 500;
  height: 50px;
  line-height: 50px;
  color: rgba(0, 0, 0, 0.85);
  margin-bottom: 20px;
}
</style>
